//
//  VisitorLoginView.swift
//  微博个人swift
//
//  Created by 李灏 on 16/2/1.
//  Copyright © 2016年 lh. All rights reserved.
//

import UIKit
import SnapKit
//1.protocol:swift中 没有 '@'
//2.遵守根协议 <NSObject>
//3.如果不遵守 NSObjectProtocol<基协议> 就无法使用 weak 关键字
//4.声明可选的协议方法 需要使用 optional关键字 是 OC中的关键字 需要在声明的协议前 加上 '@objc'
//代理传值 协议
//制定协议 设定代理对象 遵守协议
@objc protocol VisitorLoginViewDelegate:NSObjectProtocol {
    optional func userWillRegist()
    
    func userWillLogin()
    
}

class VisitorLoginView: UIView {
    
    //声明代理 swift属性 默认的属性关键字 就是 strong
    //声明弱引用的属性 需要 weak
    weak var visitorLoginViewDelegate: VisitorLoginViewDelegate?
    
    //MARK:按钮的点击事件
    @objc private func loginBtnDidClick() {
        visitorLoginViewDelegate?.userWillLogin()
    }
    
    @objc private func registerBtnDidClick() {
        visitorLoginViewDelegate?.userWillRegist?()
    }
    
    //准备外界调用的接口
    func setupUIWithInfo(imageName: String?,tipText: String) {
        if let img = imageName {
            cicleIcon.image = UIImage(named: img)
            //隐藏大房子
            bigHouse.hidden = true
            bringSubviewToFront(cicleIcon)
        }else{
            //在首页
            startAnimation()
        }
        tipLabel.text = tipText
    }
    //给圈圈加动画
    func startAnimation() {
        //基础动画
        let base = CABasicAnimation(keyPath: "transform.rotation")
        //设置重复次数和持续时间
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        base.duration = 10
        //设置动画完成/ 页面失去活跃状态  不移除图层
        base.removedOnCompletion = false
        //添加动画
        cicleIcon.layer.addAnimation(base, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置UI
        setupUI()
    }
    //默认实现报错
    //当程序员通过 sb / xib 加载该视图 程序就会崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI() {
      
        addSubview(cicleIcon)
        //遮住圈圈
        addSubview(backView)
        addSubview(bigHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registBtn)
        
        //手动添加自动布局
        cicleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            //垂直居中添加偏移量
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        bigHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cicleIcon.snp_center)
        }
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cicleIcon)
            make.top.equalTo(cicleIcon.snp_bottom).offset(16)
            //约束宽度
            make.width.equalTo(225)
            //防止页面跳动
            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        registBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        backView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(registBtn.snp_bottom)

        }
        //灰色
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        //添加点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }

    //懒加载子视图
    //背景
    lazy var backView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //圈圈
    lazy var cicleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    lazy var bigHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示文案
    lazy var tipLabel:UILabel = UILabel(text: "登陆后", fontSize: 14, textColor: UIColor.darkGrayColor())
//    {
//        let l = UILabel()
//        l.font = UIFont.systemFontOfSize(14)
//        l.text = "登陆后"
//        //换行
//        l.numberOfLines = 0
//        l.textColor = UIColor.darkGrayColor()
//        l.textAlignment = .Center
//        //设置大小
//        l.sizeToFit()
//        return l
//    }()
    //登录按钮

    lazy var loginBtn:UIButton = UIButton(backImageName: "common_button_white_disable", title: "登录", textColor: UIColor.darkGrayColor())
    
//    {
//        let btn = UIButton()
//        btn.setTitle("登录", forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
//        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
////        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//        let image = UIImage(named: "common_button_white_disable")!
//        let w = Int(image.size.width * 0.5)
//        let h = Int(image.size.height * 0.5)
//        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
//        return btn
//    }()
    //注册按钮
    lazy var registBtn:UIButton = UIButton(backImageName: "common_button_white_disable", title: "注册", textColor: UIColor.orangeColor())
    
//    {
//        let btn = UIButton()
//        btn.setTitle("注册", forState: .Normal)
//        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
//        let image = UIImage(named: "common_button_white_disable")!
//        let w = Int(image.size.width * 0.5)
//        let h = Int(image.size.height * 0.5)
//        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
//
//        return btn
//    }()
}
